from flask import Blueprint
from model.model import House,Order
import arrow
from config import DAYS
from asyncio import sleep,run

back = Blueprint('back',__name__)

@back.route('/stacisics/occupency')
def occupency():
    year_now = arrow.now().year
    start_t = arrow.get(f'{year_now}-01-01 00:00:00','YYYY-MM-DD HH:mm:ss')
    endin_t = arrow.get(f'{year_now}-12-31 24:00:00','YYYY-MM-DD HH:mm:ss')
    bettwon = endin_t - start_t
    total = House.query.count() * bettwon.days
    orders = Order.query.filter(Order.start_time < endin_t.int_timestamp,Order.end_time > start_t.int_timestamp).all()
    days = 0
    for order in orders:
        days += (order.end_time - order.start_time) // DAYS
        run(task99())
    return '%.3f'%(days * 100/ total) + '%'

async  def task99():
    await  sleep(3)

